package com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.view.textview.EllipsezeTextView;
import com.banciyuan.bcywebview.biz.detail.goods.GoodsDetailActivity;
import com.banciyuan.bcywebview.utils.currency.CurrencyUtil;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by squall on 2017/3/22.
 */

public class GoodsDiscoverHolder extends BaseDiscoverHolder {

    private ImageView content;
    private TextView goodsName;
    private TextView goodsPrice;
    private EllipsezeTextView intro;

    public GoodsDiscoverHolder(View itemView) {
        super(itemView);
        content = (ImageView) itemView.findViewById(R.id.ground_content_img);
        goodsName = (TextView) itemView.findViewById(R.id.ground_goods_name_tv);
        goodsPrice = (TextView) itemView.findViewById(R.id.price_tv);
        intro = (EllipsezeTextView) itemView.findViewById(R.id.ground_intro_tv);
    }

    @Override
    public void renderData(final Timeline mItem, final Context mContext) {
        super.renderData(mItem, mContext);
        ImageLoader.getInstance().displayImage(mItem.getContentAddress(), content, ImageOptions.optionsDetail);
        if (!TextUtils.isEmpty(mItem.getTitle())) {
            ((View) (goodsName.getParent())).setVisibility(View.VISIBLE);
            goodsName.setText(Html.fromHtml(mItem.getTitle()).toString());
        } else {
            ((View) (goodsName.getParent())).setVisibility(View.GONE);
        }
        setRecommendIntro(mContext, intro, mItem, StringUtil.htmlDecoder(mItem.getIntro()), mContext.getString(R.string.post_goods));
        if (!TextUtils.isEmpty(mItem.getPrice())) {
            goodsPrice.setText(String.format(mContext.getString(R.string.price_format), CurrencyUtil.getMoney(mItem.getPrice())));
        }
        intro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, GoodsDetailActivity.class, mItem.getZg_id());
            }
        });
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, GoodsDetailActivity.class, mItem.getZg_id());
            }
        });
    }

    private void setRecommendIntro(Context mContext, EllipsezeTextView tvIntro, Timeline mItem, String intro, String defaultIntro) {
        String finalIntro = TextUtils.isEmpty(intro) ? defaultIntro : intro;
        finalIntro = String.format(mContext.getResources().getString(R.string.card_recommended_user_format), mItem.getOUName(), finalIntro);
        tvIntro.setText(finalIntro, mItem.getOUName(), mItem.getOuid(), true);
        tvIntro.setVisibility(View.VISIBLE);
    }
}
